package cn.passlink.bletoothchat.Tools.Emoji;
import android.content.Context;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.widget.TextViewCompat;
import androidx.emoji.bundled.BundledEmojiCompatConfig;
import androidx.emoji.text.EmojiCompat;


public class EmojiUtils {

    public static void initEmojiCompat(Context context) {
        // 初始化EmojiCompat库
        EmojiCompat.init(new BundledEmojiCompatConfig(context));
    }

    public static void applyEmojiCompat(TextView textView, int minTextSize, int maxTextSize, int stepGranularity) {
        // 将EmojiCompat库应用于TextView
        TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(
                textView, minTextSize, maxTextSize, stepGranularity, TypedValue.COMPLEX_UNIT_SP);
    }

    public static void setOnClickEmojiListener(TextView textView, final OnClickEmojiListener listener) {
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CharSequence text = textView.getText();
                if (text instanceof EmojiSpannableStringBuilder) {
                    EmojiSpannableStringBuilder emojiText = (EmojiSpannableStringBuilder) text;
                    listener.onClickEmoji(emojiText.getClickedEmoji());
                } else {
                    Toast.makeText(v.getContext(), "No emoji found", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public interface OnClickEmojiListener {
        void onClickEmoji(String emoji);
    }
}